package com.wzx.ptplatform.controller.university;

import com.wzx.ptplatform.domain.*;
import com.wzx.ptplatform.service.CompanyService;
import com.wzx.ptplatform.service.StudentService;
import com.wzx.ptplatform.service.UniversityService;
import com.wzx.ptplatform.utils.ConstUtils;
import com.wzx.ptplatform.utils.LayuiPager;
import com.wzx.ptplatform.utils.LayuiUtils;
import com.wzx.ptplatform.utils.MapUtils;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author 紫煦桉
 */
@Api(tags = "学校模块")
@RestController
public class UniversityIndexController {

    final UniversityService universityService;
    final StudentService studentService;
    final CompanyService companyService;

    public UniversityIndexController(UniversityService universityService, StudentService studentService, CompanyService companyService) {
        this.universityService = universityService;
        this.studentService = studentService;
        this.companyService = companyService;
    }

    @GetMapping("university_index")
    public ModelAndView universityIndex() {
        return new ModelAndView("university/index");
    }

    @GetMapping("university_Info")
    public ModelAndView universityInfo(String id, Model model) {
        model.addAttribute("university", universityService.findById(Long.parseLong(id)));
        return new ModelAndView("university/university_info");
    }

    @GetMapping("university_forgetPassword")
    public ModelAndView universityForgetPassword(String id, Model model) {
        model.addAttribute("university", new University(Long.parseLong(id)));
        return new ModelAndView("university/forget_password");
    }

    @PostMapping("university_reset_password")
    public Map<String, Object> universityResetPassword(String id, String password) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (universityService.resetPassword(new University(Long.parseLong(id), password))) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @GetMapping("university_student_message")
    public ModelAndView universityStudentMessage(String id, Model model) {
        model.addAttribute("university", universityService.findById(Long.parseLong(id)));
        return new ModelAndView("university/student_message");
    }

    @RequestMapping("university_student_message_list")
    public LayuiPager<Student> universityStudentMessageList(
            String universityId,
            String studentName, String majorName, String cityName,
            @RequestParam(defaultValue = "1", value = "page") Integer page,
            @RequestParam(defaultValue = "10", value = "limit") Integer limit) {
        Student student = new Student();
        student.setUniversity(new University(Long.parseLong(universityId)));
        if (studentName != null && !"".equals(studentName)) {
            student.setStudentName(studentName);
        }
        if (majorName != null && !ConstUtils.PLEASE_CHOOSE.equals(majorName)) {
            student.setMajor(new Major(Long.parseLong(majorName)));
        }
        if (cityName != null && !ConstUtils.PLEASE_CHOOSE.equals(cityName)) {
            student.setCity(new City(Long.parseLong(cityName)));
        }
        Page<Student> pager = studentService.findList(student, page - 1, limit);
        LayuiPager<Student> layuiPager = LayuiUtils.initPager(page, limit);
        LayuiUtils.setPager(layuiPager, pager.getContent(), Math.toIntExact(pager.getTotalElements()));
        return layuiPager;
    }

    @GetMapping("university_student_pt")
    public ModelAndView universityStudentPt(String id, Model model) {
        model.addAttribute("university", universityService.findById(Long.parseLong(id)));
        return new ModelAndView("university/pt/student_pt");
    }

    @RequestMapping("university_student_pt_list")
    public LayuiPager<Student> universityStudentPtList(
            String universityId,
            String studentName, String majorName, String cityName,
            @RequestParam(defaultValue = "1", value = "page") Integer page,
            @RequestParam(defaultValue = "10", value = "limit") Integer limit) {
        Student student = new Student();
        student.setUniversity(new University(Long.parseLong(universityId)));
        if (studentName != null && !"".equals(studentName)) {
            student.setStudentName(studentName);
        }
        if (majorName != null && !ConstUtils.PLEASE_CHOOSE.equals(majorName)) {
            student.setMajor(new Major(Long.parseLong(majorName)));
        }
        if (cityName != null && !ConstUtils.PLEASE_CHOOSE.equals(cityName)) {
            student.setCity(new City(Long.parseLong(cityName)));
        }
        Page<Student> pager = studentService.findPtList(student, page - 1, limit);
        LayuiPager<Student> layuiPager = LayuiUtils.initPager(page, limit);
        LayuiUtils.setPager(layuiPager, pager.getContent(), Math.toIntExact(pager.getTotalElements()));
        return layuiPager;
    }

    @GetMapping("university_student_pt_company_message")
    public ModelAndView universityStudentPtCompanyMessage(String id, Model model) {
        Student student = studentService.findById(Long.parseLong(id));
        Company company = companyService.findById(student.getCompany().getId());
        model.addAttribute("company", company);
        return new ModelAndView("university/pt/student_pt_company");
    }
}
